package net.alteiar.ui;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JPanel;

import net.alteiar.images.IDrawable;

public class BasicPanelImage<E extends IDrawable> extends JPanel implements
		Observer {
	private static final long serialVersionUID = 1L;

	private E img;

	public BasicPanelImage(E img) {
		this.img = img;
		this.img.addObserver(this);

		this.setPreferredSize(new Dimension(img.getWidth(), img.getHeight()));
	}

	public void setImage(E img) {
		this.img.deleteObserver(this);
		this.img = img;
		this.img.addObserver(this);
		this.setPreferredSize(new Dimension(img.getWidth(), img.getHeight()));
		this.revalidate();
		this.repaint();
	}

	public E getImage() {
		return img;
	}

	@Override
	public void paint(Graphics g) {
		img.draw((Graphics2D) g);
	}

	@Override
	public void update(Observable o, Object arg) {
		this.revalidate();
		this.repaint();
	}
}
